/**
 * Created by wushanxin on 14-4-8.
 */


var log = require("../lib/log");
var config = require('../config');
var crypto = require('crypto');
var RUtil = require('./dataformatUtil');
var viruser=require('../models/viruser.js');

exports.valid = function(request, response) {
    var method = request.method || '';



    if(method.toUpperCase() === "GET") {
        if(typeof(request.query.fieldId)!=="undefined" && request.query.fieldId!=="")
        {
            if(request.query.fieldId=="username")
            {
                var retFlag=false;
                viruser.select(request.query.fieldValue, function(error,results) {
                    if((error)!== null)
                    {
                        // 数据库异常
                        retFlag=true;
                        response.send("[\""+request.query.fieldId+"\","+retFlag+"]");
                    } else if(results.length <= 0) {
                        // 不存在，直接响应页面
                        retFlag=true;
                        response.send("[\""+request.query.fieldId+"\","+retFlag+"]");
                    } else {
                        //存在数据
                        retFlag=false;
                        response.send("[\""+request.query.fieldId+"\","+retFlag+"]");
                    }


                });

            }
            else if(request.query.fieldId=="email")
            {
                var retFlag=false;
                viruser.selectByEmail(request.query.fieldValue, function(error,results) {
                    if((error)!== null)
                    {
                        // 数据库异常
                        retFlag=true;
                        response.send("[\""+request.query.fieldId+"\","+retFlag+"]");
                    } else if(results.length <= 0) {
                        // 不存在，直接响应页面
                        retFlag=true;
                        response.send("[\""+request.query.fieldId+"\","+retFlag+"]");
                    } else {
                        //存在数据
                        retFlag=false;
                        response.send("[\""+request.query.fieldId+"\","+retFlag+"]");
                    }


                });

            }

        }
        else
        {
            response.send("[\""+request.query.fieldId+"\",false]");
        }
    }


};
